/**
 * 
 */
package com.lvan.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author lvan.he<br>
 *         2010-11-18<br>
 * @version 1.0.0.0
 */
public class DateUtil {
    public static final String DATAFORMAT_YEAR = "yyyy";
    public static final String DATAFORMAT_YEARMONTH = "yyyy-MM";
    public static final String DATAFORMAT_SIMPLE = "yyyy-MM-dd";
    public static final String DATAFORMAT_LESSMM = "yyyy-MM-dd HH:mm";
    public static final String DATAFORMAT_ALL = "yyyy-MM-dd HH:mm:ss";

    public static Date toDate(String pattern, String date) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static Date format(int type, Date date) {
        Calendar c = Calendar.getInstance();
        if(date!=null)
            c.setTime(date);
        c.set(Calendar.MILLISECOND, 0);
        c.set(Calendar.SECOND, 0);
        if(type==Calendar.SECOND)
            return c.getTime();
        c.set(Calendar.MINUTE, 0);
        if(type==Calendar.MINUTE)
            return c.getTime();
        c.set(Calendar.HOUR, 0);
        if(type==Calendar.HOUR)
            return c.getTime();
        c.set(Calendar.DAY_OF_MONTH, 1);
        if(type==Calendar.DAY_OF_MONTH)
            return c.getTime();
        c.set(Calendar.MONTH, 0);
        if(type==Calendar.MONTH)
            return c.getTime();
        return null;
    }

    public static String toString(String pattern, Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static String getSysCurrentTime() {
        Date date = new Date();
        try {
            return toString(DATAFORMAT_ALL, date);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 获得本周一的Calendar
     * 
     * @return
     */
    public static Calendar getMondayOfThisWeekOfCalendar() {
        Calendar c = Calendar.getInstance();
        int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (dayofweek == 0)
            dayofweek = 7;
        c.add(Calendar.DATE, -dayofweek + 1);
        return c;
    }

    /**
     * 获得本周一的日期
     * 
     * @return
     */
    public static String getMondayOfThisWeek() {
        Calendar c = getMondayOfThisWeekOfCalendar();
        return toString(DATAFORMAT_SIMPLE, c.getTime());
    }

    /**
     * 获得本周日的Calendar
     * 
     * @return
     */
    public static Calendar getSundayOfThisWeekOfCalendar() {
        Calendar c = Calendar.getInstance();
        int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (dayofweek == 0)
            dayofweek = 7;
        c.add(Calendar.DATE, -dayofweek + 7);
        return c;
    }

    /**
     * 获得本周日的日期
     * 
     * @return
     */
    public static String getSundayOfThisWeek() {
        Calendar c = getSundayOfThisWeekOfCalendar();
        return toString(DATAFORMAT_SIMPLE, c.getTime());
    }

    /**
     * 根据日期，获得指定的上下周一
     * 
     * @param date
     *            日期
     * @param nextweek
     *            上下周 上周：-1，下周为1，类推
     * @return
     */
    public static String getNextMondayOfThisWeek(Date date, int nextweek) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayofweek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (dayofweek == 0)
            dayofweek = 7;
        calendar.add(Calendar.DATE, -dayofweek + nextweek * 7 + 1);
        return toString(DATAFORMAT_SIMPLE, calendar.getTime());
    }

    public static String getNextSundayOfThisWeek(Date date, int nextweek) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (dayofweek == 0)
            dayofweek = 7;
        c.add(Calendar.DATE, -dayofweek + nextweek * 7 + 7);
        return toString(DATAFORMAT_SIMPLE, c.getTime());
    }

    /**
     * 获得今天属于第几周
     * 
     * @return
     */
    public static int getTotalWeekOfYear(int nextweek) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, nextweek * 7);
        return c.get(Calendar.WEEK_OF_YEAR);
    }
    
    public static Long differOfHour(Date date) {
        if(date==null)
            return null;
        return Math.abs(new Date().getTime()-date.getTime())/(1000*60*60);
    }
    public static Long differOfDay(Date date) {
        if(date==null)
            return null;
        return Math.abs(new Date().getTime()-date.getTime())/(1000*60*60*24);
    }
    public static Long differOfMinute(Date date) {
        if(date==null)
            return null;
        return Math.abs(new Date().getTime()-date.getTime())/(1000*60);
    }
    
}
